﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Data
{

    /// <summary>
    /// SqlServerConnection构建者 
    /// </summary>
    public sealed class SqlServerConnectionStringBuilder : BaseConnectionStringBuilder
    {

        /// <summary>
        /// 连接超时等待时间
        /// </summary>
        [Description("获取或设置在终止尝试并产生错误之前，等待与服务器连接的时间长度（以秒为单位）")]
        public int ConnectionTimeout { get; set; }


        /// <summary>
        /// 是否在连接中 集成用户ID和密码等安全信息
        /// </summary>
        [Description("获取或设置一个布尔值，该值指示是否在连接中指定用户 ID 和密码（值为 false 时），或者是否使用当前的 Windows 帐户凭据进行身份验证（值为 true 时）")]
        public bool IntegratedSecurity { get; set; }


        /// <summary>
        /// 是创建进程内连接还是进程外连接
        /// </summary>
        [Description("获取或设置一个值，该值指示应建立与 SQL Server 的客户端/服务器连接还是进程内连接")]
        public bool ContextConnection { get; set; }
 

        /// <summary>
        /// 数据源  SqlServer的实例名称或者网址
        /// </summary>
        [Description("获取或设置要连接到的 SQL Server 实例的名称或网络地址")]
        public string DataSource { get; set; }


        /// <summary>
        /// 初始化数据库名称
        /// </summary>
        [Description("获取或设置与该连接关联的数据库的名称")]
        public string InitialCatalog { get; set; }


        /// <summary>
        /// 是否启用数据库连接池
        /// </summary>
        [Description("获取或设置一个布尔值，该值指示每次请求连接时该连接是汇入连接池还是显式打开。")]
        public bool Pooling { get; set; } = true;


        /// <summary>
        /// 最小的连接池大小 
        /// </summary>
        [Description("获取或设置针对此特定连接字符串连接池中所允许的最小连接数。")]
        public int MinPoolSize { get; set; }


        /// <summary>
        /// 最大的连接大小
        /// </summary>
        [Description("获取或设置针对此特定连接字符串连接池中所允许的最大连接数。")]
        public int MaxPoolSize { get; set; } = 100;


        /// <summary>
        /// 连接字符串
        /// </summary>
        public override string ConnectionString
        {
            get
            {
                SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
                if (this.ContextConnection)
                {
                    sqlConnectionStringBuilder.ContextConnection = true;
                }
                else if (this.IntegratedSecurity)
                {
                    sqlConnectionStringBuilder.IntegratedSecurity = true;
                    sqlConnectionStringBuilder.DataSource = this.DataSource;
                    sqlConnectionStringBuilder.InitialCatalog = this.InitialCatalog;
                    sqlConnectionStringBuilder.ConnectTimeout = this.ConnectionTimeout;
                    sqlConnectionStringBuilder.Pooling = this.Pooling;
                    sqlConnectionStringBuilder.MinPoolSize = this.MinPoolSize;
                    sqlConnectionStringBuilder.MaxPoolSize = this.MaxPoolSize;
                }
                else
                {
                    sqlConnectionStringBuilder.UserID = base.UserID;
                    sqlConnectionStringBuilder.Password = (base.Password ?? "");
                    sqlConnectionStringBuilder.DataSource = this.DataSource;
                    sqlConnectionStringBuilder.InitialCatalog = this.InitialCatalog;
                    sqlConnectionStringBuilder.ConnectTimeout = this.ConnectionTimeout;
                    sqlConnectionStringBuilder.Pooling = this.Pooling;
                    sqlConnectionStringBuilder.MinPoolSize = this.MinPoolSize;
                    sqlConnectionStringBuilder.MaxPoolSize = this.MaxPoolSize;
                }
                return sqlConnectionStringBuilder.ConnectionString;
            }
            set
            {
                SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
                try
                {
                    sqlConnectionStringBuilder.ConnectionString = value;
                }
                catch
                {
                }
                this.ContextConnection = sqlConnectionStringBuilder.ContextConnection;
                this.IntegratedSecurity = sqlConnectionStringBuilder.IntegratedSecurity;
                base.UserID = sqlConnectionStringBuilder.UserID;
                base.Password = sqlConnectionStringBuilder.Password;
                this.DataSource = sqlConnectionStringBuilder.DataSource;
                this.InitialCatalog = sqlConnectionStringBuilder.InitialCatalog;
                this.ConnectionTimeout = sqlConnectionStringBuilder.ConnectTimeout;
                this.Pooling = sqlConnectionStringBuilder.Pooling;
                this.MinPoolSize = sqlConnectionStringBuilder.MinPoolSize;
                this.MaxPoolSize = sqlConnectionStringBuilder.MaxPoolSize;
            }
        }


    }
}
